home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / PointSprites / PointSprites.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  24.7 KB  |  606 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Point Sprites"
  4.    ClientHeight    =   4050
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5055
  8.    Icon            =   "PointSprites.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   270
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   337
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "Form1"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  20. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       PointSprites.frm
  22. '  Content:    Sample showing how to use point sprites to do particle effects
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. Option Compare Text
  26. Private Type CUSTOMVERTEX
  27.     v As D3DVECTOR
  28.     color As Long
  29.     tu As Single
  30.     tv As Single
  31. End Type
  32. Const D3DFVF_COLORVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_TEX1)
  33. Const GROUND_GRIDSIZE = 8
  34. Const GROUND_WIDTH = 256
  35. Const GROUND_HEIGHT = 256
  36. Const GROUND_TILE = 32
  37. Const GROUND_COLOR = &HBBEEEEEE
  38. Private Enum PARTICLE_COLORS
  39.     COLOR_WHITE = 0
  40.     COLOR_RED = 1
  41.     COLOR_GREEN = 2
  42.     COLOR_BLUE = 3
  43.     NUM_COLORS = 4
  44. End Enum
  45. Dim g_clrColor(4) As D3DCOLORVALUE
  46. Dim g_clrColorFade(4) As D3DCOLORVALUE
  47. Dim m_media As String
  48. Dim m_ParticleSystem As CParticle
  49. Dim m_ParticleTexture As Direct3DTexture8
  50. Dim m_NumParticlesToEmit As Long
  51. Dim m_bStaticParticle As Boolean
  52. Dim m_nParticleColor As Long
  53. Dim m_GroundTexture As Direct3DTexture8
  54. Dim m_NumGroundVertices As Long
  55. Dim m_NumGroundIndices As Long
  56. Dim m_GroundIB As Direct3DIndexBuffer8
  57. Dim m_GroundVB As Direct3DVertexBuffer8
  58. Dim m_planeGround As D3DPLANE
  59. Dim m_bDrawReflection As Boolean
  60. Dim m_bCanDoAlphaBlend  As Boolean
  61. Dim m_bCanDoClipPlanes  As Boolean
  62. Dim m_bDrawHelp As Boolean
  63. Dim m_matView As D3DMATRIX
  64. Dim m_matOrientation As D3DMATRIX
  65. Dim m_vPosition As D3DVECTOR
  66. Dim m_vVelocity As D3DVECTOR
  67. Dim m_fYaw              As Single
  68. Dim m_fYawVelocity      As Single
  69. Dim m_fPitch            As Single
  70. Dim m_fPitchVelocity    As Single
  71. Dim m_fElapsedTime As Single
  72. Dim m_bKey(256) As Boolean
  73. Dim g_fTime As Single
  74. Dim g_fLastTime As Single
  75. Dim m_grVerts() As CUSTOMVERTEX
  76. Dim m_grVerts2() As CUSTOMVERTEX
  77. Dim m_binit As Boolean
  78. Dim m_bMinimized As Boolean
  79. Dim m_bStopSim As Boolean
  80. Const kMaxParticles = 128
  81. Const kParticleRadius = 0.01
  82. '-----------------------------------------------------------------------------
  83. ' Name: Form_KeyPress()
  84. ' Desc:
  85. '-----------------------------------------------------------------------------
  86. Private Sub Form_KeyPress(KeyAscii As Integer)
  87.     If Chr$(KeyAscii) = "r" Then m_bDrawReflection = Not m_bDrawReflection
  88. End Sub
  89. '-----------------------------------------------------------------------------
  90. ' Name: Form_Load()
  91. ' Desc:
  92. '-----------------------------------------------------------------------------
  93. Private Sub Form_Load()
  94.     Me.Show
  95.     DoEvents
  96.     'setup defaults
  97.     Init
  98.     ' Initialize D3D
  99.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  100.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  101.     ' If all fail it will display a message box indicating so.
  102.     '
  103.     m_binit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  104.     If Not (m_binit) Then End
  105.     ' find Media and set media path
  106.     m_media = FindMediaDir("ground2.bmp")
  107.     D3DUtil_SetMediaPath m_media
  108.     ' Set initial state
  109.     OneTimeSceneInit
  110.     ' Load Mesh and textures from media
  111.     InitDeviceObjects
  112.     ' Set device render states, lighting, camera
  113.     RestoreDeviceObjects
  114.     ' Start Timer
  115.     DXUtil_Timer TIMER_start
  116.     ' Start our timer
  117.     DXUtil_Timer TIMER_start
  118.     ' Run the simulation forever
  119.     ' See Form_Keydown for exit processing
  120.     Do While True
  121.         ' Increment the simulation
  122.         FrameMove
  123.         
  124.         ' Render one image of the simulation
  125.         If Render Then
  126.             
  127.             ' Present the image to the screen
  128.             D3DUtil_PresentAll g_focushwnd
  129.         End If
  130.         
  131.         ' Allow for events to get processed
  132.         DoEvents
  133.         
  134.     Loop
  135. End Sub
  136. '-----------------------------------------------------------------------------
  137. ' Name: Form_KeyDown()
  138. ' Desc: Process key messages for exit and change device
  139. '-----------------------------------------------------------------------------
  140. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  141.      
  142.      m_bKey(KeyCode) = True
  143.      
  144.      Select Case KeyCode
  145.         
  146.         Case vbKeyEscape
  147.             Unload Me
  148.             
  149.         Case vbKeyF2
  150.                 
  151.             ' Pause the timer
  152.             DXUtil_Timer TIMER_STOP
  153.             m_bStopSim = True
  154.             ' Bring up the device selection dialog
  155.             ' we pass in the form so the selection process
  156.             ' can make calls into InitDeviceObjects
  157.             ' and RestoreDeviceObjects
  158.             frmSelectDevice.SelectDevice Me
  159.             
  160.             ' Restart the timer
  161.             m_bStopSim = False
  162.             DXUtil_Timer TIMER_start
  163.             
  164.         Case vbKeyReturn
  165.         
  166.             ' Check for Alt-Enter if not pressed exit
  167.             If Shift <> 4 Then Exit Sub
  168.             
  169.             ' stop simulation
  170.             DXUtil_Timer TIMER_STOP
  171.             m_bStopSim = True
  172.             
  173.             ' If we are windowed go fullscreen
  174.             ' If we are fullscreen returned to windowed
  175.             If g_d3dpp.Windowed Then
  176.                  D3DUtil_ResetFullscreen
  177.             Else
  178.                  D3DUtil_ResetWindowed
  179.             End If
  180.                              
  181.             ' Call Restore after ever mode change
  182.             ' because calling reset looses state that needs to
  183.             ' be reinitialized
  184.             RestoreDeviceObjects
  185.            
  186.             ' Restart simulation
  187.             DXUtil_Timer TIMER_STOP
  188.             m_bStopSim = False
  189.     End Select
  190. End Sub
  191. '-----------------------------------------------------------------------------
  192. ' Name: Form_KeyUp()
  193. ' Desc: Process key messages for exit and change device
  194. '-----------------------------------------------------------------------------
  195. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  196.     m_bKey(KeyCode) = False
  197. End Sub
  198. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  199.     DXUtil_Timer (TIMER_STOP)
  200.     m_bStopSim = True
  201. End Sub
  202. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  203.    DXUtil_Timer (TIMER_start)
  204.     m_bStopSim = False
  205. End Sub
  206. '-----------------------------------------------------------------------------
  207. ' Name: Form_Resize()
  208. ' Desc: hadle resizing of the D3D backbuffer
  209. '-----------------------------------------------------------------------------
  210. Private Sub Form_Resize()
  211.     ' If D3D is not initialized then exit
  212.     If Not m_binit Then Exit Sub
  213.     ' If we are in a minimized state stop the timer and exit
  214.     If Me.WindowState = vbMinimized Then
  215.         DXUtil_Timer TIMER_STOP
  216.         m_bMinimized = True
  217.         m_bStopSim = True
  218.         Exit Sub
  219.         
  220.     ' If we just went from a minimized state to maximized
  221.     ' restart the timer
  222.     Else
  223.         If m_bMinimized = True Then
  224.             DXUtil_Timer TIMER_start
  225.             m_bMinimized = False
  226.             m_bStopSim = False
  227.         End If
  228.     End If
  229.             
  230.     ' Dont let the window get too small
  231.     If Me.ScaleWidth < 10 Then
  232.         Me.width = Screen.TwipsPerPixelX * 10
  233.         Exit Sub
  234.     End If
  235.     If Me.ScaleHeight < 10 Then
  236.         Me.height = Screen.TwipsPerPixelY * 10
  237.         Exit Sub
  238.     End If
  239.     m_ParticleSystem.DeleteDeviceObjects
  240.     Set m_ParticleSystem = Nothing
  241.     Set m_ParticleSystem = New CParticle
  242.     'reset and resize our D3D backbuffer to the size of the window
  243.     D3DUtil_ResizeWindowed Me.hwnd
  244.     'All state get losts after a reset so we need to reinitialze it here
  245.     RestoreDeviceObjects
  246.     DXUtil_Timer TIMER_STOP
  247.     m_ParticleSystem.Init kMaxParticles, kParticleRadius
  248.     m_ParticleSystem.InitDeviceObjects g_dev
  249.     DXUtil_Timer TIMER_RESET
  250. End Sub
  251. '-----------------------------------------------------------------------------
  252. ' Name: Form_Unload()
  253. ' Desc:
  254. '-----------------------------------------------------------------------------
  255. Private Sub Form_Unload(Cancel As Integer)
  256.     DeleteDeviceObjects
  257.     End
  258. End Sub
  259. '-----------------------------------------------------------------------------
  260. ' Name: Init()
  261. ' Desc: Constructor
  262. '-----------------------------------------------------------------------------
  263. Sub Init()
  264.     Me.Caption = "PointSprites: Using particle effects"
  265.         
  266.        
  267.     Set m_ParticleSystem = New CParticle
  268.     m_ParticleSystem.Init kMaxParticles, kParticleRadius
  269.     Set m_ParticleTexture = Nothing
  270.     m_NumParticlesToEmit = 10
  271.     m_bStaticParticle = True
  272.     m_nParticleColor = COLOR_WHITE
  273.     Set m_GroundTexture = Nothing
  274.     m_NumGroundVertices = (GROUND_GRIDSIZE + 1) * (GROUND_GRIDSIZE + 1)
  275.     m_NumGroundIndices = (GROUND_GRIDSIZE * GROUND_GRIDSIZE) * 6
  276.     Set m_GroundVB = Nothing
  277.     Set m_GroundIB = Nothing
  278.     m_planeGround = D3DPLANE4(0, 1, 0, 0)
  279.     m_bDrawReflection = False
  280.     m_bCanDoAlphaBlend = False
  281.     m_bCanDoClipPlanes = False
  282.     m_bDrawHelp = False
  283.     m_vPosition = vec3(0, 3, -4)
  284.     m_vVelocity = vec3(0, 0, 0)
  285.     m_fYaw = 0
  286.     m_fYawVelocity = 0
  287.     m_fPitch = 0.5
  288.     m_fPitchVelocity = 0
  289.     g_clrColor(0) = ColorValue4(1, 1, 1, 1)
  290.     g_clrColor(1) = ColorValue4(1, 0.5, 0.5, 1)
  291.     g_clrColor(2) = ColorValue4(0.5, 1, 0.5, 1)
  292.     g_clrColor(3) = ColorValue4(0.125, 0.5, 1, 1)
  293.     g_clrColorFade(0) = ColorValue4(1, 0.25, 0.25, 1)
  294.     g_clrColorFade(1) = ColorValue4(1, 0.25, 0.25, 1)
  295.     g_clrColorFade(2) = ColorValue4(0.25, 0.75, 0.25, 1)
  296.     g_clrColorFade(3) = ColorValue4(0.125, 0.25, 0.75, 1)
  297. End Sub
  298. '-----------------------------------------------------------------------------
  299. ' Name: OneTimeSceneInit()
  300. ' Desc: Called during initial app startup, this function performs all the
  301. '       permanent initialization.
  302. '-----------------------------------------------------------------------------
  303. Sub OneTimeSceneInit()
  304.     D3DXMatrixTranslation m_matView, 0, 0, 10
  305.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  306. End Sub
  307. '-----------------------------------------------------------------------------
  308. ' Name: FrameMove()
  309. ' Desc: Called once per frame, the call is the entry point for animating
  310. '       the scene.
  311. '-----------------------------------------------------------------------------
  312. Sub FrameMove()
  313.             
  314.     If m_bStopSim = True Then Exit Sub
  315.         
  316.     g_fTime = DXUtil_Timer(TIMER_GETAPPTIME) * 1.3
  317.     m_fElapsedTime = g_fTime - g_fLastTime
  318.     g_fLastTime = g_fTime
  319.     If m_fElapsedTime < 0 Then Exit Sub
  320.         
  321.     ' Slow things down for the REF device
  322.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  323.     Dim fSpeed As Single
  324.     Dim fAngularSpeed
  325.     fSpeed = 5 * m_fElapsedTime
  326.     fAngularSpeed = 1 * m_fElapsedTime
  327.     ' Slowdown the camera movement
  328.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  329.     m_fYawVelocity = m_fYawVelocity * 0.9
  330.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  331.     ' Process keyboard input
  332.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  333.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  334.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  335.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  336.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  337.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  338.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  339.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  340.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  341.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  342.     If (m_bKey(vbKeyAdd)) Then
  343.         If (m_NumParticlesToEmit < 10) Then m_NumParticlesToEmit = m_NumParticlesToEmit + 1
  344.     End If
  345.     If (m_bKey(vbKeySubtract)) Then
  346.         If (m_NumParticlesToEmit > 0) Then m_NumParticlesToEmit = m_NumParticlesToEmit - 1
  347.     End If
  348.     ' Update the position vector
  349.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  350.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  351.     D3DXVec3Add vT, vT, vTemp
  352.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  353.     D3DXVec3Add m_vPosition, m_vPosition, vT
  354.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  355.     ' Update the yaw-pitch-rotation vector
  356.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  357.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  358.     If (m_fPitch < 0) Then m_fPitch = 0
  359.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  360.     Dim qR As D3DQUATERNION, det As Single
  361.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  362.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  363.     D3DXMatrixInverse m_matView, det, m_matOrientation
  364.     ' Update particle system
  365.     If (m_bStaticParticle) Then
  366.         m_ParticleSystem.Update m_fElapsedTime, m_NumParticlesToEmit, _
  367.                          g_clrColor(m_nParticleColor), _
  368.                          g_clrColorFade(m_nParticleColor), 8, _
  369.                          vec3(0, 0, 0)
  370.     Else
  371.         m_ParticleSystem.Update m_fElapsedTime, m_NumParticlesToEmit, _
  372.                          g_clrColor(m_nParticleColor), _
  373.                          g_clrColorFade(m_nParticleColor), 8, _
  374.                          vec3(3 * Sin(g_fTime), 0, 3 * Cos(g_fTime))
  375.     End If
  376. End Sub
  377. '-----------------------------------------------------------------------------
  378. ' Name: Render()
  379. ' Desc: Called once per frame, the call is the entry point for 3d
  380. '       rendering. This function sets up render states, clears the
  381. '       viewport, and renders the scene.
  382. '-----------------------------------------------------------------------------
  383. Function Render() As Boolean
  384.     Dim v As CUSTOMVERTEX
  385.     Dim hr As Long
  386.      'See what state the device is in.
  387.     Render = False
  388.     hr = g_dev.TestCooperativeLevel
  389.     If hr = D3DERR_DEVICENOTRESET Then
  390.         g_dev.Reset g_d3dpp
  391.         RestoreDeviceObjects
  392.     End If
  393.     'dont bother rendering if we are not ready yet
  394.     If hr <> 0 Then Exit Function
  395.     Render = True
  396.     ' Clear the backbuffer
  397.     D3DUtil_ClearAll &HFF&
  398.     With g_dev
  399.         .BeginScene
  400.                 
  401.         
  402.         ' Draw reflection of particles
  403.         If (m_bDrawReflection) Then
  404.             Dim matReflectedView As D3DMATRIX
  405.             
  406.             D3DXMatrixReflect matReflectedView, m_planeGround
  407.             D3DXMatrixMultiply matReflectedView, matReflectedView, m_matView
  408.             .SetTransform D3DTS_VIEW, matReflectedView
  409.             'Dim clipplane As D3DCLIPPLANE
  410.             'LSet clipplane = m_planeGround
  411.             '.SetClipPlane 0, clipplane
  412.             .SetRenderState D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0
  413.             ' Draw particles
  414.             .SetTexture 0, m_ParticleTexture
  415.             .SetRenderState D3DRS_ZWRITEENABLE, 0 'FALSE
  416.             .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'TRUE
  417.             m_ParticleSystem.Render g_dev
  418.             .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  419.             .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  420.             .SetRenderState D3DRS_CLIPPLANEENABLE, 0 'FALSE
  421.             .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'True
  422.             .SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
  423.             .SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
  424.             .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
  425.         End If
  426.         .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  427.         .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  428.         .SetRenderState D3DRS_CLIPPLANEENABLE, 0 'FALSE
  429.         .SetRenderState D3DRS_ALPHABLENDENABLE, 1 '1 'True
  430.         .SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
  431.         .SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
  432.         .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
  433.         
  434.         
  435.         ' Draw ground
  436.         .SetTransform D3DTS_VIEW, m_matView
  437.         .SetTexture 0, m_GroundTexture
  438.         .SetVertexShader D3DFVF_COLORVERTEX
  439.         .SetStreamSource 0, m_GroundVB, Len(v)
  440.         .SetIndices m_GroundIB, 0
  441.         .DrawIndexedPrimitive D3DPT_TRIANGLELIST, _
  442.                                             0, m_NumGroundVertices, _
  443.                                             0, (m_NumGroundIndices / 3)
  444.         ' Draw particles
  445.         .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'True
  446.         .SetRenderState D3DRS_SRCBLEND, D3DBLEND_ONE
  447.         .SetRenderState D3DRS_DESTBLEND, D3DBLEND_ONE
  448.         .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
  449.         .SetRenderState D3DRS_ZWRITEENABLE, 0 'False
  450.         .SetRenderState D3DRS_ZENABLE, 1 'TRUE
  451.         .SetTexture 0, m_ParticleTexture
  452.         .SetRenderState D3DRS_ZENABLE, 0  'False
  453.         .SetTexture 0, m_ParticleTexture
  454.         m_ParticleSystem.Render g_dev
  455.         .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  456.         .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  457.         .EndScene
  458.     End With
  459. End Function
  460. '-----------------------------------------------------------------------------
  461. ' Name: InitDeviceObjects()
  462. ' Desc: Initialize scene objects.
  463. '-----------------------------------------------------------------------------
  464. Function InitDeviceObjects() As Boolean
  465.     Dim i As Long
  466.     Dim v As CUSTOMVERTEX
  467.     Set m_GroundTexture = D3DUtil_CreateTexture(g_dev, "Ground2.bmp", D3DFMT_UNKNOWN)
  468.     Set m_ParticleTexture = D3DUtil_CreateTexture(g_dev, "Particle.bmp", D3DFMT_UNKNOWN)
  469.           
  470.     ' Check if we can do the reflection effect
  471.     m_bCanDoAlphaBlend = ((g_d3dCaps.SrcBlendCaps And D3DPBLENDCAPS_SRCALPHA) = D3DPBLENDCAPS_SRCALPHA) And _
  472.                          ((g_d3dCaps.DestBlendCaps And D3DPBLENDCAPS_INVSRCALPHA) = D3DPBLENDCAPS_INVSRCALPHA)
  473.     m_bCanDoClipPlanes = (g_d3dCaps.MaxUserClipPlanes >= 1)
  474.     ' Note: all HW with Software Vertex Processing can do clipplanes
  475.     m_bCanDoClipPlanes = True
  476.         
  477.     If (m_bCanDoAlphaBlend And m_bCanDoClipPlanes) Then m_bDrawReflection = True
  478.     ' Create ground object
  479.         
  480.     ' Create vertex buffer for ground object
  481.     Set m_GroundVB = g_dev.CreateVertexBuffer(m_NumGroundVertices * Len(v), _
  482.                       0, D3DFVF_COLORVERTEX, D3DPOOL_MANAGED)
  483.         
  484.     ' Fill vertex buffer
  485.      Dim zz As Long, xx As Long
  486.      
  487.      ReDim m_grVerts(GROUND_GRIDSIZE * GROUND_GRIDSIZE * 6)
  488.      
  489.      
  490.      i = 0
  491.      For zz = 0 To GROUND_GRIDSIZE
  492.         For xx = 0 To GROUND_GRIDSIZE
  493.             
  494.             m_grVerts(i).v.x = GROUND_WIDTH * ((xx / GROUND_GRIDSIZE) - 0.5)
  495.             m_grVerts(i).v.y = 0
  496.             m_grVerts(i).v.z = GROUND_HEIGHT * ((zz / GROUND_GRIDSIZE) - 0.5)
  497.             m_grVerts(i).color = GROUND_COLOR
  498.             m_grVerts(i).tu = xx * (GROUND_TILE / GROUND_GRIDSIZE)
  499.             m_grVerts(i).tv = zz * (GROUND_TILE / GROUND_GRIDSIZE)
  500.             i = i + 1
  501.         Next
  502.     Next
  503.     D3DVertexBuffer8SetData m_GroundVB, 0, Len(v) * (GROUND_GRIDSIZE + 1) * (GROUND_GRIDSIZE + 1), 0, m_grVerts(0)
  504.     Dim vtx As Long
  505.     Dim m_Indices() As Integer
  506.     ReDim m_Indices(m_NumGroundIndices * 4)
  507.     Dim z As Long, x As Long
  508.     ' Create the index buffer
  509.     Set m_GroundIB = g_dev.CreateIndexBuffer(m_NumGroundIndices * 2, _
  510.                             0, _
  511.                             D3DFMT_INDEX16, D3DPOOL_MANAGED)
  512.             
  513.     ' Fill in indices
  514.     i = 0
  515.     For z = 0 To GROUND_GRIDSIZE - 1
  516.         For x = 0 To GROUND_GRIDSIZE - 1
  517.                 
  518.                 vtx = x + z * (GROUND_GRIDSIZE + 1)
  519.                 m_Indices(i) = vtx + 1: i = i + 1
  520.                 m_Indices(i) = vtx + 0: i = i + 1
  521.                 m_Indices(i) = vtx + 0 + (GROUND_GRIDSIZE + 1): i = i + 1
  522.                 m_Indices(i) = vtx + 1: i = i + 1
  523.                 m_Indices(i) = vtx + 0 + (GROUND_GRIDSIZE + 1): i = i + 1
  524.                 m_Indices(i) = vtx + 1 + (GROUND_GRIDSIZE + 1): i = i + 1
  525.             
  526.         Next
  527.     Next
  528.     D3DIndexBuffer8SetData m_GroundIB, 0, 2 * m_NumGroundIndices, 0, m_Indices(0)
  529.     ' Initialize the particle system
  530.     m_ParticleSystem.InitDeviceObjects g_dev
  531.         
  532.     InitDeviceObjects = True
  533. End Function
  534. '-----------------------------------------------------------------------------
  535. ' Name: VerifyDevice()
  536. '-----------------------------------------------------------------------------
  537. Function VerifyDevice(Behavior As Long, format As CONST_D3DFORMAT) As Boolean
  538.     ' Make sure device can do ONE:ONE alphablending
  539.     If (0 = (g_d3dCaps.SrcBlendCaps And D3DPBLENDCAPS_ONE) = D3DPBLENDCAPS_ONE) Then Exit Function
  540.     If (0 = (g_d3dCaps.DestBlendCaps And D3DPBLENDCAPS_ONE) = D3DPBLENDCAPS_ONE) Then Exit Function
  541.         
  542.     ' We will run this app using software vertex processing
  543.     If (Behavior = D3DCREATE_HARDWARE_VERTEXPROCESSING) Then Exit Function
  544.     VerifyDevice = True
  545. End Function
  546. '-----------------------------------------------------------------------------
  547. ' Name: DeleteDeviceObjects()
  548. ' Desc: Called when the app is exitting, or the device is being changed,
  549. '       this function deletes any device dependant objects.
  550. '-----------------------------------------------------------------------------
  551. Sub DeleteDeviceObjects()
  552.     Set m_GroundTexture = Nothing
  553.     Set m_ParticleTexture = Nothing
  554.     Set m_GroundVB = Nothing
  555.     Set m_GroundIB = Nothing
  556.     If (m_ParticleSystem Is Nothing) Then Exit Sub
  557.     m_ParticleSystem.DeleteDeviceObjects
  558.     m_binit = False
  559. End Sub
  560. '-----------------------------------------------------------------------------
  561. ' Name: FinalCleanup()
  562. ' Desc: Called before the app exits, this function gives the app the chance
  563. '       to cleanup after itself.
  564. '-----------------------------------------------------------------------------
  565. Sub FinalCleanup()
  566.     Set m_GroundTexture = Nothing
  567.     Set m_ParticleTexture = Nothing
  568.     Set m_ParticleSystem = Nothing
  569. End Sub
  570. '-----------------------------------------------------------------------------
  571. ' Name: InvalidateDeviceObjects()
  572. ' Desc: Place code to release non managed objects here
  573. '-----------------------------------------------------------------------------
  574. Sub InvalidateDeviceObjects()
  575.     'all objects are managed in this sample
  576. End Sub
  577. '-----------------------------------------------------------------------------
  578. ' Name: RestoreDeviceObjects()
  579. ' Desc:
  580. '-----------------------------------------------------------------------------
  581. Sub RestoreDeviceObjects()
  582.     ' Set the world matrix
  583.     Dim matWorld As D3DMATRIX
  584.     D3DXMatrixIdentity matWorld
  585.     g_dev.SetTransform D3DTS_WORLD, matWorld
  586.     ' Set projection matrix
  587.     Dim matProj As D3DMATRIX
  588.     D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, Me.ScaleHeight / Me.ScaleWidth, 0.1, 100
  589.     g_dev.SetTransform D3DTS_PROJECTION, matProj
  590.     ' Set renderstates
  591.     With g_dev
  592.         Call .SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR)
  593.         Call .SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR)
  594.         Call .SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR)
  595.         Call .SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
  596.         Call .SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1)
  597.         Call .SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE)
  598.         Call .SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE)
  599.         Call .SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE)
  600.         Call .SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE)
  601.         Call .SetRenderState(D3DRS_LIGHTING, 0)     'FALSE
  602.         Call .SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)
  603.         Call .SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT)
  604.     End With
  605. End Sub
  606.